using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Math;
using Dogfight2008.Common;
using OpenTK.OpenGL;

namespace Dogfight2008
{
  public class Shot : ICollideable, IActive, IRenderable
  {
    public double Speed;
    public readonly double Angle;
    public Vector2 Position;
    double lastDeltaMS;
    Countdown harmless;

    public Shot(Vector2 position, double radians, double mPerMs, double harmlessTime)
    {
      Position = new Vector2(position);
      Angle = radians;
      Speed = mPerMs;
      harmless = new Countdown(harmlessTime);
    }

    public void Tick(double ms)
    {
      harmless.Tick(ms);
      lastDeltaMS = ms;
      Position +=
        Calc.v(
        Math.Cos(Angle), Math.Sin(Angle)) *
          (float)(ms*Speed);
    }

    public Box2d GetBox2d()
    {
      if (harmless.Stopped)
        return new Box2d(Position.X, Position.Y, 0, Speed * lastDeltaMS, -0.01, 0.01, Angle);
      else
        return new Box2d(-1000, -1000, 1, 1, 1, 1, 0);
    }

    public void Die()
    {
      Speed = 0;
    }

    public void Render(IRenderer renderer)
    {
      Vector3 pos3d = new Vector3(Position.X, -20, Position.Y);
      renderer.PushMatrix();
      renderer.Translate(pos3d);
      renderer.Rotate(-Vector3.UnitY, Angle);
      renderer.Rotate(Vector3.UnitZ, -Math.PI / 2);
      renderer.PushMatrix();
      if (harmless.Stopped)
        renderer.DrawMesh(Mesh.RedPlane);
      else
        renderer.DrawMesh(Mesh.Shot);
      renderer.PopMatrix();
      renderer.PopMatrix();
    }
  }
}
